package assignment6;

import java.util.Scanner;
import java.util.TreeMap;

/**
 * Data structure for Morse Code
 * 
 * @author Terry Wasserleben CS204-21370 Prof. Jeannette Myers Due: 11/02/2011
 */
public class MorseCodeTree {
	
	/**
	 * mcTree contains the Morse code to English translation
	 */
	public TreeMap<String, Character> mCode;
	
	/**
	 * Constructor for MorseCodeTree
	 */
	public MorseCodeTree() {
		mCode = new TreeMap<String, Character>(new MorseCodeComparator());
		create_mcTree();
	}
	
	/**
	 * method (should only be) called by constructor but mainly to clean up the
	 * constructor
	 */
	private void create_mcTree() {
		mCode.put(new String(""), ' ');
		// Alphabet
		mCode.put(new String(".-"), 'a');
		mCode.put(new String("-..."), 'b');
		mCode.put(new String("-.-."), 'c');
		mCode.put(new String("-.."), 'd');
		mCode.put(new String("."), 'e');
		mCode.put(new String("..-."), 'f');
		mCode.put(new String("--."), 'g');
		mCode.put(new String("...."), 'h');
		mCode.put(new String(".."), 'i');
		mCode.put(new String(".---"), 'j');
		mCode.put(new String("-.-"), 'k');
		mCode.put(new String(".-.."), 'l');
		mCode.put(new String("--"), 'm');
		mCode.put(new String("-."), 'n');
		mCode.put(new String("---"), 'o');
		mCode.put(new String(".--."), 'p');
		mCode.put(new String("--.-"), 'q');
		mCode.put(new String(".-."), 'r');
		mCode.put(new String("..."), 's');
		mCode.put(new String("-"), 't');
		mCode.put(new String("..-"), 'u');
		mCode.put(new String("...-"), 'v');
		mCode.put(new String(".--"), 'w');
		mCode.put(new String("-..-"), 'x');
		mCode.put(new String("-.--"), 'y');
		mCode.put(new String("--.."), 'z');
		// Punctuation
		mCode.put(new String(".----."), '\'');
		mCode.put(new String(".-.-.-"), '.');
		mCode.put(new String("--..--"), ',');
		mCode.put(new String("..--.."), '?');
		mCode.put(new String("---..."), ':');
		mCode.put(new String("-....-"), '-');
		mCode.put(new String("-..-."), '/');
		mCode.put(new String(".--.-."), '@');
		mCode.put(new String("-...-"), '=');
	}
	
	/**
	 * getWord method constructs the word and returns it
	 * 
	 * @param tempString String of Morse code passed in to translate into
	 *            English word
	 * @return String returned of the final formed word
	 */
	public String getWord(String tempString) {
		String word = "";
		Scanner in = new Scanner(tempString);
		in.useDelimiter(" ");
		while (in.hasNext()) {
			word += getLetter(in.next());
		}
		return word;
	}
	
	/**
	 * method reads string and then returns the correct letter and if it doesn't
	 * know a certain code a "[IDK]" is returned
	 * 
	 * @param tempString Morse code String passed in
	 * @return letter in String format returned
	 */
	public String getLetter(String tempString) {
		if (!mCode.containsKey(tempString)) {
			return "[IDK]";
		}
		return mCode.get(tempString).toString();
	}
	
}
